// 获取cookie
export const getCookie = (cname) =>{
  let name = cname + "=";
  let ca = document.cookie.split(';');
  for(let i=0; i<ca.length; i++) 
  {
    let c = ca[i].trim();
    if (c.indexOf(name)==0) return c.substring(name.length,c.length).toLocaleLowerCase()
  }
  return "";
}

// 设置cookie
export const setCookie = (cname,cvalue,exdays) =>{
  const d = new Date();
  d.setTime(d.getTime()+(exdays*24*60*60*1000));
  const expires = "expires="+d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}

//名称统一校验
export const nameCheck = (rule, value, callback) => {
  const regText = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/g;
  if(value.length>22){
    return callback(new Error("名称不能超过22个字"));
  }else if (!regText.test(value)) {
    return callback(new Error("名称不能输入特殊字符"));
  } else {
    callback();
  }
};
// 长度校验
export const lengthCheck = (rule, value, callback) => {
  if(value.length>50){
    return callback(new Error("不能超过50个字"));
  }else {
    callback();
  }
};
// 邮箱校验
export const emailCheck = (rule, value, callback) => {
  const regText =/^[a-zA-Z0-9_-]+(\+?.[a-zA-Z0-9_-]+)+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  if(!regText.test(value)){
    return callback(new Error("邮箱格式错误"));
  }else {
    callback();
  }
};

// 判断是否有当前id，如果有返回id，如果没有直接清空
export const checkHasId = (arr, id, tag = 'id') => {
  let flag = true
  if(arr.length == 0){
    flag = true;
  }else{
    for (let i = 0; i < arr.length; i++) {
      if (arr[i][tag] == id) {
        flag = false;
        break;
      }
    }
  }
  if(flag){
    id = ''
  }
  return id
};
// 判断是否有当前id，返回数组
export const checkHasIdArr = (parentArr, childArr) => {
  let tempArr=[];
  if(childArr && childArr.length!=0){
    childArr.forEach(item=>{
      if(checkHasId(parentArr,item)){
        tempArr.push(item)
      }
    })
  }
  return tempArr;
};
export const processPassword=(pw)=>{
  let Base64 = require('js-base64').Base64;
  var str = Base64.encode(pw);
  if(str.length>2){
    var lp = str.substr(0,2);
    var rp = str.substr(2,str.length);
    pw = rp+lp;
  }
  return pw;
};
export const pxToRemScale=(px)=>{
  let fontSizeValue=document.documentElement.style.fontSize
  if(fontSizeValue){
    fontSizeValue=fontSizeValue.substring(0,fontSizeValue.length-2)
  }
  return px/fontSizeValue;
}

// 高度
export const scrollHeight = (document.documentElement.clientHeight || document.body.clientHeight) - 60 - 30 -32 -56 ;
